package com.system.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.user.dao.BindDao;
import com.system.user.dao.ThresholdDao;
import com.system.user.dao.WaterDao;
import com.system.user.entity.WaterEntity;
import com.system.user.entityVO.WaterVO;
import com.system.user.service.WaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service("waterService")
public class WaterServiceImpl implements WaterService {

    @Autowired
    private WaterDao waterDao;

    @Autowired
    private ThresholdDao thresholdDao;

    @Autowired
    private BindDao bindDao;

    @Override
    public List<WaterEntity> selectAll() {
        List<WaterEntity> waterEntities = waterDao.selectAll();
        return waterEntities;
    }

    @Override
    public List<WaterEntity> selectBySiteOrNumber(WaterEntity waterEntity) {
        List<WaterEntity> waterEntities = waterDao.selectBySiteOrNumber(waterEntity);
        return waterEntities;
    }

    @Override
    public List<WaterVO> findWaterVOByUId(Integer uId, Integer number) {
        // number为1时查找最新一条，不为1时查找所有记录
        return waterDao.selectByUId(uId, number);
    }

    @Override
    public List<WaterVO> findAllByBNumber(String bNumber, String createTime) {
        List<WaterVO> list = waterDao.selectAllByBNumber(bNumber);
        if (createTime != null && !createTime.equals("")) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (list.get(0).equals(createTime)) {
                return list;
            }
            List<WaterVO> newList = new ArrayList<>();
            for (int i = 1; i < list.size(); i++) {
                String time = simpleDateFormat.format(list.get(i).getCreateTime());
                if (time.equals(createTime)) {
                    newList.add(list.get(i));
                    list.remove(i);
                    break;
                }
            }
            for (WaterVO w : list) {
                newList.add(w);
            }
            return newList;
        }
        return list;
    }

    @Override
    public String findSite(String bNumber) {
        return waterDao.selectSiteByNumber(bNumber);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addWater(WaterEntity water) throws Exception {
        int is_success = waterDao.insertWater(water);
        if (is_success > 0) {
            water.setWaterNumber("w" + water.getId());
            int is_success1 = waterDao.updateWater(water);
            if (is_success1 > 0) {
                if (thresholdDao.insertThreshold(water.getWaterNumber(), null) > 0) {
                    return true;
                }
            }
        }
        throw new Exception();
    }

    @Override
    public boolean updateWater(WaterEntity water) {
        int is_success = waterDao.updateWater(water);
        if (is_success > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        WaterEntity water = waterDao.selectById(id);
        if (waterDao.deleteWater(id) > 0) {
            if (thresholdDao.deleteThreshold(water.getWaterNumber()) > 0) {
                bindDao.deleteBindByBNumber(water.getWaterNumber());
                return true;
            }
        }
        return false;
    }
}
